@include('admin._include.header')
<style>
    .sku_item {
        display: none;
    }

    .sku_item_image {
        text-align: center;
    }

    .layui-upload-img {
        margin-top: 20px;
    }

    .sku_item_image img {
        width: 36px;
        height: 36px;
        border-radius: 3px;
        cursor: pointer;
        background: #ddd;
    }

    .sku-choose {
        width: 800px !important;
    }

    .sku-choose span {
        display: block;
        line-height: 40px;
        height: 40px;
    }

    .sku-choose span i {
        color: #333;
        cursor: pointer;
    }

    .sku-choose .layui-btn-primary {
        padding: 0 0 0 15px !important;
    }

    .sku-choose .layui-btn-primary i {
        float: right;
        color: #333;
        margin-left: 15px;
    }

    .sku-choose .add-sku-input {
        height: 28px;
        line-height: 28px;
        margin-left: 5px;
        padding-left: 10px;
        border: 1px solid #ddd;
        border-radius: 3px;
        vertical-align: middle;
        font-size: 13px;
    }

    .sku-choose .add-sku-input:hover {
        border: 1px solid #009688;
    }

    .sku-choose .add-sku-btn {
        border-radius: 0 3px 3px 0 !important;
        margin-left: -44px;
    }
    .spu_item{
        margin-bottom:10px;
    }
    .spu_item input{
        width: 190px;
        vertical-align: middle;
        margin-right: 10px;
        display: inline-flex;
    }
</style>
<body class="auth-user">
<div class="layui-fluid">
    <div class="layui-row larryms-panel auth-user-add">
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline" style="width: 450px">
                    <input type="text" name="goods_name" placeholder="请填写商品名称" value="{{$model->goods_name}}"
                           autocomplete="off" class="layui-input larry-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商品品牌</label>
                <div class="layui-input-inline">
                    <select name="branch_id" lay-verify="required">
                        <option value="">请选择一个品牌</option>
                        @foreach($branch as $item)
                            <option value="{{$item->id}}"
                                    @if($model->branch_id == $item->id) selected @endif>{{$item->branch_name}}</option>
                        @endforeach
                    </select>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">商品分类</label>

                @if($model->cat_id)
                    <div class="layui-input-inline">
                        <select name="" id="first_category" lay-verify="required" lay-filter="choose_category">
                            <option value="">请选择一级分类</option>
                            @foreach($category as $item)
                                <option value="{{$item->id}}"
                                        @if($model->first_cat_id == $item->id) selected @endif>{{$item->category_name}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="cat_id" id="second_category" lay-verify="required">
                            <option value="">请选择二级分类</option>
                            @foreach($children_category as $item)
                                <option value="{{$item->id}}"
                                        @if($model->cat_id == $item->id) selected @endif>{{$item->category_name}}</option>
                            @endforeach
                        </select>

                    </div>
                @else
                    <div class="layui-input-inline">
                        <select name="" id="first_category" lay-verify="required" lay-filter="choose_category">
                            <option value="">请选择一级分类</option>
                            @foreach($category as $item)
                                <option value="{{$item->id}}">{{$item->category_name}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="cat_id" id="second_category" lay-verify="required">
                            <option value="">请选择二级分类</option>
                        </select>

                    </div>
                @endif
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">商品主图</label>

                <div class="layui-input-inline">
                    <button type="button" class="layui-btn images_upload">上传图片</button>
                    <img class="layui-upload-img" src="{{$model->images}}" style="width: 100px;"
                         id="test-upload-normal-img">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline" style="width: 450px">
                    <input type="number" name="sort" placeholder="排序由大到小" value="{{$model->sort}}" autocomplete="off"
                           class="layui-input larry-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商品简介</label>
                <div class="layui-input-inline" style="width: 500px">
                    <textarea class="layui-textarea" style="width: 500px;resize:none"
                              name="goods_description">{{$model->goods_description}}</textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商品参数</label>
                <div class="layui-input-inline" style="width: 500px">
                   <div class="spu_item">
                       <input type="text" class="layui-input" placeholder="参数名称">
                       <input type="text" class="layui-input" placeholder="参数值">
                       <button  class="layui-btn add-param" type="button" data-item="{{count($spu)}}">添加</button>
                   </div>
                    @if(count($spu))
                        @foreach($spu as $k => $item)
                            <div class='spu_item'>
                                <input type='text' name='spu[{{$k}}][spu_name]' disabled class='layui-input' value='{{$item->spu_name}}'/>
                                <input type='text' name='spu[{{$k}}][spu_value]' disabled class='layui-input' value='{{$item->spu_value}}'/>
                                <button class='layui-btn layui-btn-danger delete-param' type='button'>删除</button>
                            </div>
                        @endforeach
                    @endif
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商品规格</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_single" lay-filter="sku" value="1" title="单规格"
                           @if($model->is_single == 1 || is_null($model->is_single)) checked="" @endif>
                    <input type="radio" name="is_single" lay-filter="sku" value="2" title="多规格"
                           @if($model->is_single == 2) checked="" @endif>
                </div>
            </div>

            <div class="layui-form-item form-sku-choose"
                 @if($model->is_single == 1 || is_null($model->is_single)) style="display: none" @endif>
                <label class="layui-form-label">选择规格</label>
                <div class="layui-input-inline form-sku-choose-select">
                    <select name="sku_id" id="sku_id" lay-verify="required" lay-filter="choose_sku">
                        <option value="0">请选择规格</option>
                        @foreach($sku as $item)
                            <option value="{{$item->id}}"
                                    @if($model->sku_id == $item->id) selected @endif >{{$item->sku_name}}</option>
                        @endforeach
                    </select>

                </div>
                <div class="layui-input-inline">
                    <button class="layui-btn" type="button">确定</button>
                </div>
            </div>

            @if($model->is_single == 2)

                @foreach($sku_detail as $item)
                    <div class="layui-form-item form-sku">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline sku-choose">
                        <span>
                            <i class='item-sku-key'>{{$item['value']}}</i>
                            <i class="layui-icon-close-fill layui-icon"></i>
                        </span>
                            @if(is_array($item['details']) && count($item['details']))
                                @foreach($item['details'] as $item2)
                                    <button class='layui-btn layui-btn-sm layui-btn-primary item-sku-value'
                                            type='button'>{{$item2}}<i class='layui-icon-close layui-icon'></i></button>
                                @endforeach
                            @endif
                            <input type="text" class="add-sku-input" placeholder="添加属性">
                            <button class="layui-btn layui-btn-sm add-sku-btn" type="button">添加</button>
                        </div>
                    </div>
                @endforeach


            @endif
            <div class="layui-form-item form-sku-generate"
                 @if($model->is_single == 1 || is_null($model->is_single)) style="display: none" @endif>
                <label class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <button class="layui-btn layui-btn-danger form-sku-generate-btn" type="button">生成</button>
                </div>
            </div>

            @if($model->is_single == 1 || is_null($model->is_single))
                <div class="layui-form-item sku-form-list">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-block">
                        <table class="layui-table">
                            <colgroup>
                                <col>
                                <col>
                                <col>
                                <col>
                                <col>
                            </colgroup>
                            <thead>
                            <tr>
                                <th>图片</th>
                                <th>原价</th>
                                <th>现价</th>
                                <th>合伙人价格</th>
                                <th>分销商价格</th>
                                <th>库存</th>
                                <th>重量 kg</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td class="sku_item_image">
                                    <img src="{{$sku_attribute->sku_images ?: asset('images/upload-icon.png')}}" class="layui-upload" alt="">
                                    <input type="hidden"  value="{{$sku_attribute->sku_images}}" name="sku[sku_images]">
                                </td>
                                <td><input type="number" name="sku[price]" class="layui-input" value="{{$sku_attribute->price}}" placeholder="请输入原价"></td>
                                <td><input type="number" name="sku[market_price]" class="layui-input"
                                         value="{{$sku_attribute->market_price}}"  placeholder="请输入现价"></td>
                                <td><input type="number" name="sku[partner_price]" class="layui-input"
                                           value="{{$sku_attribute->partner_price}}"  placeholder="请输入合伙人价格"></td>
                                <td><input type="number" name="sku[distributor_price]" class="layui-input"
                                           value="{{$sku_attribute->distributor_price}}"   placeholder="请输入分销商价格"></td>
                                <td><input type="number" name="sku[stock]" class="layui-input"    value="{{$sku_attribute->stock}}" placeholder="请输入库存"></td>
                                <td><input type="number" name="sku[weight]" class="layui-input"    value="{{$sku_attribute->weight}}" placeholder="请输入商品重量">
                                </td>
                            </tr>

                            </tbody>
                        </table>
                    </div>
                </div>
            @else

                <div class="layui-form-item sku-form-list">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-block">
                        <table class="layui-table">

                            <thead>
                            <tr>
                                @foreach($sku_detail as $k2 => $item2)
                                    <th>{{$item2['value']}}</th>
                                    <input type='hidden' name='sku[sku_detail][{{$k2}}]' value='{{$item2['value']}}'>
                                @endforeach
                                <th>图片</th>
                                <th>原价</th>
                                <th>现价</th>
                                <th>合伙人价格</th>
                                <th>分销商价格</th>
                                <th>库存</th>
                                <th>重量 kg</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($sku_attribute as $k => $item)
                                <tr>
                                    @foreach($item['attribute_array'] as $k3 => $item3)
                                        <td>
                                            <input class='layui-input' type="text" value="{{$item3[array_key_first($item3)]}}"
                                                   name="sku[{{$k}}][attribute_name][{{$k3}}]" placeholder="">
                                        </td>
                                    @endforeach
                                    <td class="sku_item_image">
                                        <img src="{{ $item['sku_images'] ?: asset('images/upload-icon.png')}}"
                                             class="layui-upload" alt="">
                                        <input type="hidden" name="sku[{{$k}}][sku_images]"
                                               value="{{$item['sku_images']}}">
                                    </td>
                                    <td><input type="number" name="sku[{{$k}}][price]" class="layui-input"
                                               placeholder="请输入原价" value="{{$item['price']}}"></td>

                                    <td><input type="number" name="sku[{{$k}}][market_price]" class="layui-input"
                                               placeholder="请输入现价" value="{{$item['market_price']}}"></td>

                                    <td><input type="number" name="sku[{{$k}}][partner_price]" class="layui-input"
                                               placeholder="请输入合伙人价格" value="{{$item['partner_price']}}"></td>

                                    <td><input type="number" name="sku[{{$k}}][distributor_price]" class="layui-input"
                                               placeholder="请输入分销商价格" value="{{$item['distributor_price']}}"></td>

                                    <td><input type="number" name="sku[{{$k}}][stock]" class="layui-input"
                                               placeholder="请输入库存" value="{{$item['stock']}}"></td>

                                    <td><input type="number" name="sku[{{$k}}][weight]" class="layui-input"
                                               placeholder="请输入商品重量" value="{{$item['weight']}}"></td>

                                </tr>
                            @endforeach

                            </tbody>
                        </table>
                    </div>
                </div>

            @endif

            <div class="layui-form-item" style="text-align: center">
                <input type="hidden" name="images" lay-verify="required" value="{{$model->images}}">
                <input type="hidden" name="id" value="{{$model->id}}">
                <button class="layui-btn" lay-submit lay-filter="ad_add">确定</button>
            </div>

        </form>
    </div>
</div>

<script>
    layui.config({
        base: "/plugin/layuiadmin/" //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'upload', 'form'], function () {
        var $ = layui.jquery;
        var upload = layui.upload;
        var form = layui.form;
        var index_upload;

        $('.add-param').on('click', function () {

            if(!$(this).prev().prev().val()){
                layer.msg('请填写参数名称');
                return;
            }

            if(!$(this).prev().val()){
                layer.msg('请填写参数值');
                return;
            }

            var i = $(this).data('item');
            var content = "<div class='spu_item'>\n" +
                "               <input type='text' name='spu["+i+"][spu_name]' disabled class='layui-input' value='"+$(this).prev().prev().val()+"' />\n" +
                "               <input type='text' name='spu["+i+"][spu_value]' disabled class='layui-input' value='"+$(this).prev().val()+"' />\n" +
                "               <button class='layui-btn layui-btn-danger delete-param' type='button'>删除</button>\n" +
                "          </div>";
            $(this).parent().after(content);

            $(this).prev().val('');
            $(this).prev().prev().val('');
            $(this).data('item', i+1);

        });

        $(document).on('click', '.delete-param', function () {
            $(this).parents('.spu_item').remove();
        });

        function renderSkuImage() {
            upload.render({
                elem: $('.layui-upload'),
                url: '{{url('api/public/upload')}}',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                multiple: false,  //是否允许多图上传
                accept: 'images',  //指定允许上传时校验的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
                acceptMime: 'image/*',
                field: 'file',  //上传文件参数名
                before: function (obj) {
                    index_upload = layer.load(1);
                },
                done: function (res) {
                    layer.close(index_upload);
                    if (res.status != SUCCESS) {
                        return layer.msg(res.message);
                    } else {
                        let that = $(this.item);
                        that.attr('src', res.data);
                        that.next().next().val(res.data)
                    }
                },
                error: function () {
                    layer.close(index_upload);
                }
            });

        }

        upload.render({
            elem: $('.images_upload'),
            url: '{{url('api/public/upload')}}',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            multiple: false,  //是否允许多图上传
            accept: 'image',  //指定允许上传时校验的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
            acceptMime: 'image/*',
            field: 'file',  //上传文件参数名
            before: function (obj) {
                index_upload = layer.load(1);
            },
            done: function (res) {
                layer.close(index_upload);
                if (res.status != SUCCESS) {
                    return layer.msg(res.message);
                } else {
                    $("input[name='images']").val(res.data);
                    $('#test-upload-normal-img').attr('src', res.data);
                }
            },
            error: function () {
                layer.close(index_upload);
            }
        });

        renderSkuImage();

        form.on('radio(sku)', function (data) {
            that = data.elem;
            $('.sku-form-list').remove();
            $('.layui-upload').remove();


            if (data.value == 1) {
                $('.form-sku').remove();
                $('.form-sku-choose').css('display', 'none');
                $('.form-sku-generate').css('display', 'none');

                form.render('select');


                $(that).parents('.layui-form-item').after("<div class=\"layui-form-item sku-form-list\">\n" +
                    "                    <label class=\"layui-form-label\"></label>\n" +
                    "                    <div class=\"layui-input-block\" >\n" +
                    "                        <table class=\"layui-table\">\n" +
                    "                            <colgroup>\n" +
                    "                                <col>\n" +
                    "                                <col>\n" +
                    "                                <col>\n" +
                    "                                <col>\n" +
                    "                                <col>\n" +
                    "                            </colgroup>\n" +
                    "                            <thead>\n" +
                    "                            <tr>\n" +
                    "                                <th>图片</th>\n" +
                    "                                <th>原价</th>\n" +
                    "                                <th>现价</th>\n" +
                    "                                <th>合伙人价格</th>\n" +
                    "                                <th>分销商价格</th>\n" +
                    "                                <th>库存</th>\n" +
                    "                                <th>重量 kg</th>\n" +
                    "                            </tr>\n" +
                    "                            </thead>\n" +
                    "                            <tbody>\n" +
                    "                            <tr>\n" +
                    "                                <td class=\"sku_item_image\">\n" +
                    "                                    <img src=\"{{asset('images/upload-icon.png')}}\" class=\"layui-upload\"  alt=\"\">\n" +
                    "                                    <input type=\"hidden\" name=\"sku[sku_images]\">\n" +
                    "                                </td>\n" +
                    "                                <td><input type=\"number\" name=\"sku[price]\" class=\"layui-input\" placeholder=\"请输入原价\"></td>\n" +
                    "                                <td><input type=\"number\" name=\"sku[market_price]\" class=\"layui-input\" placeholder=\"请输入现价\"></td>\n" +
                    "                                <td><input type=\"number\" name=\"sku[partner_price]\" class=\"layui-input\" placeholder=\"请输入合伙人价格\"></td>\n" +
                    "                                <td><input type=\"number\" name=\"sku[distributor_price]\" class=\"layui-input\" placeholder=\"请输入分销商价格\"></td>\n" +
                    "                                <td><input type=\"number\" name=\"sku[stock]\" class=\"layui-input\" placeholder=\"请输入库存\"></td>\n" +
                    "                                <td><input type=\"number\" name=\"sku[weight]\" class=\"layui-input\" placeholder=\"请输入商品重量\"></td>\n" +
                    "                            </tr>\n" +
                    "\n" +
                    "                            </tbody>\n" +
                    "                        </table>\n" +
                    "                    </div>\n" +
                    "                </div>")
            } else {
                $('.form-sku-choose').css('display', 'block');
                $('.form-sku-generate').css('display', 'block');
            }

            renderSkuImage();

        });

        form.on('select(choose_category)', function (data) {
            $.ajax({
                url: "{{url('goods/category-tree')}}",
                data: {'category_id': data.value},
                dataType: "json",
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: "post",
                success: function (resultData) {
                    $("#second_category").empty();
                    if (resultData.status === SUCCESS) {
                        $("#second_category").append(new Option("请选择二级分类", ""));
                        $.each(resultData.data, function (index, item) {
                            $('#second_category').append(new Option(item.category_name, item.id));
                        });
                    } else {
                        $("#second_category").append(new Option("请选择二级分类", ""));
                    }

                    form.render("select");
                }
            });
        });

        $(document).on('click', '.layui-icon-close-fill', function () {
            $(this).parents('.form-sku').remove();
        });

        $(document).on('click', '.layui-icon-close', function () {
            $(this).parent().remove();
        });

        $(document).on('click', '.add-sku-btn', function () {
            var val = $(this).prev().val();

            if (!val) {
                layer.msg('请填写属性信息');
                return false;
            }

            $(this).prev().before("<button class='layui-btn layui-btn-sm layui-btn-primary item-sku-value' type='button'>" + val + "<i class='layui-icon-close layui-icon'></i></button>");

            $(this).prev().val('');
        });

        form.on('select(choose_sku)', function (data) {
            var sku_id = data.value;

            if (sku_id) {
                $.ajax({
                    url: "{{url('sku/detail')}}",
                    type: "post",
                    data: {'id': sku_id},
                    dataType: "json",
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function (res) {
                        if (res.status == SUCCESS) {
                            $('.form-sku').remove();
                            if (res.data.sku_value) {
                                let sku_value = res.data.sku_value;
                                let content = '';
                                $.each(sku_value, function (k, v) {
                                    content += "<div class=\"layui-form-item form-sku\">\n" +
                                        "                <label class=\"layui-form-label\"></label>\n" +
                                        "                <div class=\"layui-input-inline sku-choose\">\n" +
                                        "                   <span><i class='item-sku-key'>" + v + "</i> <i class=\"layui-icon-close-fill layui-icon\"></i> </span>\n" +
                                        "                    <input type=\"text\" class=\"add-sku-input\" placeholder=\"添加属性\">\n" +
                                        "                    <button class=\"layui-btn layui-btn-sm add-sku-btn\" type=\"button\">添加</button>\n" +
                                        "                </div>\n" +
                                        "            </div>"
                                });

                                $('.form-sku-generate').before(content);
                            }
                        }
                    }
                });
            } else {
                $('.form-sku').remove();
            }

        });


        $('.form-sku-generate-btn').on('click', function () {
            var arr = [];
            var arr_title = [];
            $('.form-sku').each(function (k, v) {

                var arr_item = [];
                $.each($(this).find('.item-sku-value'), function () {
                    arr_item.push($(this).text());
                });
                arr_title.push($(this).find('.item-sku-key').text());
                arr[k] = arr_item;
            });

            if (arr.length <= 0) {
                return false;
            }

            $('.sku-form-list').remove();
            $('.layui-upload').remove();

            var result = combination(arr);

            var content = '    <div class="layui-form-item  sku-form-list">\n' +
                '                <label class="layui-form-label"></label>\n' +
                '                <div class="layui-input-block" >\n' +
                '                    <table class="layui-table">\n' +
                '                        <thead>\n' +
                '                        <tr>';
            $.each(arr_title, function (k, v) {
                content += "<th>" + v + "</th> <input type='hidden' name='sku[sku_detail][" + k + "]' value='" + v + "'> "
            });

            content += " <th>图片</th>\n" +
                "                            <th>原价</th>\n" +
                "                            <th>现价</th>\n" +
                "                            <th>合伙人价格</th>\n" +
                "                            <th>分销商价格</th>\n" +
                "                            <th>库存</th>\n" +
                "                            <th>重量 kg</th>\n" +
                "                        </tr>\n" +
                "                        </thead>\n" +
                "                        <tbody>\n";

            $.each(result, function (k, v) {
                content += "<tr>";
                for (var j = 0; j < v.length; j++) {
                    content += "<td><input type='text' name='sku[" + k + "][attribute_name][" + [j] + "]' class='layui-input' value='" + v[j] + "' ></td> "
                }
                content += "<td class=\"sku_item_image\">\n" +
                    "                                <img src=\"{{asset('images/upload-icon.png')}}\" class=\"layui-upload\"  alt=\"\">\n" +
                    "                                <input type=\"hidden\" name=\"sku[" + k + "][sku_images]\">\n" +
                    "                            </td>\n" +
                    "                            <td><input type=\"number\" name=\"sku[" + k + "][price]\" class=\"layui-input\" placeholder=\"请输入原价\"></td>\n" +
                    "                            <td><input type=\"number\" name=\"sku[" + k + "][market_price]\" class=\"layui-input\" placeholder=\"请输入现价\"></td>\n" +
                    "                            <td><input type=\"number\" name=\"sku[" + k + "][partner_price]\" class=\"layui-input\" placeholder=\"请输入合伙人价格\"></td>\n" +
                    "                            <td><input type=\"number\" name=\"sku[" + k + "][distributor_price]\" class=\"layui-input\" placeholder=\"请输入分销商价格\"></td>\n" +
                    "                            <td><input type=\"number\" name=\"sku[" + k + "][stock]\" class=\"layui-input\" placeholder=\"请输入库存\"></td>\n" +
                    "                            <td><input type=\"number\" name=\"sku[" + k + "][weight]\" class=\"layui-input\" placeholder=\"请输入商品重量\"></td>\n" +
                    "                        </tr>\n";

            });

            content += "</tbody></table></div></div>";
            $('.form-sku-generate').after(content);
            renderSkuImage();

        });

        function combination(arr) {
            var sarr = [[]];
            var result = [];
            for (var i = 0; i < arr.length; i++) {
                var tarr = [];
                for (var j = 0; j < sarr.length; j++) {
                    for (var k = 0; k < arr[i].length; k++) {
                        tarr.push(sarr[j].concat(arr[i][k]));
                    }
                }
                sarr = tarr;
            }
            for (var m = 0; m < sarr.length; m++) {
                result.push(sarr[m]);
            }
            return result;
        }

        form.on('submit(ad_add)', function (data) {

            var index = layer.load(1);
            $.ajax({
                url: "{{url('goods/edit-goods')}}",
                dateType: 'json',
                data: data.field,
                type: 'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function (res) {
                    layer.close(index);
                    layer.msg(res.message);
                    if (res.status == SUCCESS) {
                        var iframe = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(iframe); //再执行关闭
                    }
                }
            });

            return false;
        });


    });
</script>

</body>
@include('admin._include.footer')